//------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
//     Copyright (c) Telligent Systems Corporation.  All rights reserved.
// </copyright> 
//------------------------------------------------------------------------------

using System;
using System.Collections;
using System.Web.UI.WebControls;
using CommunityServer.Components;
using CommunityServer.ControlPanel.UI;
using CommunityServer.Controls;
using CA = ComponentArt.Web.UI;

namespace CommunityServer.ControlPanel.Files
{
	public class FileCategoryList : BaseFilesGridControl
	{
		bool pageHasLoaded = false;
		protected LinkButton New;
		protected CA.Grid Grid1;
		protected Modal Modal1;
		protected ModalLink NewCategory;
		
		private int categoryID = CSContext.Current.GetIntFromQueryString("cid",-1);
		protected CommunityServer.ControlPanel.Controls.ResourceControl ResourceControl1;
	
		public int CategoryID
		{
			get{return categoryID;}
			set
			{
				categoryID = value;
				if(pageHasLoaded)
				{
					buildGrid();
					Grid.DataBind();
				}
			}
		}

		private const string EditCategoryUrl = "CategoryForm.aspx?Sectionid={0}&ct={1}&categoryid={2}";
		//private const string DeleteCategoryUrl = "BlogCommand.aspx?SectionID={0}&Command={1}&CategoryID={2}&ct={3}";
		
		private void Page_Load(object sender, EventArgs e)
		{
			NewCategory.Url = string.Format(EditCategoryUrl,CurrentFolder.SectionID,"-1","-1");

			if(!Page.IsPostBack && !FilesPage.IsCallBack)
			{
				JavaScript.RegisterRefresh(this.Page);
				buildGrid();
				Grid.DataBind();
			}
			pageHasLoaded=true;
		}

		private void DeleteCategory(int categoryID)
		{
			if(categoryID > 0)
			{
				PostCategory pc = PostCategories.GetCategory(categoryID,this.CurrentFolder.SectionID,true);
				
				if(pc.SectionID != CurrentFolder.SectionID)
					throw new CSException(CSExceptionType.SectionNotFound);

				if(pc != null)
					PostCategories.DeleteCategory(pc.CategoryID,pc.SectionID);
			}
		}


		protected override void buildGrid()
		{

			ArrayList al = PostCategories.GetCategories(CurrentFolder.SectionID,false,true, -1);
			Grid.DataSource = al;

			this.RecordCount = al.Count;

			base.buildGrid();

		}

		protected override void ConfigureGrid()
		{
			AddGridCheckMarkTemplate("IsEnabled");
			AddGridHrefTemplate(@"filelist.aspx?cid=##  DataItem.GetMember(""CategoryID"").Text ##", "TotalThreads") ;
			AddGridHrefTemplate(@"javascript:Telligent_Modal.Open(""CategoryForm.aspx?sectionid=##  DataItem.GetMember(""SectionID"").Text ##&categoryID=## DataItem.GetMember(""CategoryID"").Text ##"", 400, 300, reloadCategories);", "Name") ;
			AddGridPagerTemplate("Name", "CategoryID", "MostRecentPostDate","IsEnabled");
		}

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
			this.Grid.DeleteCommand += new CA.Grid.GridItemEventHandler(this.Grid_DeleteCommand);
		}
		
		/// <summary>
		///		Required method for Designer support - do not modify
		///		the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.Load += new System.EventHandler(this.Page_Load);

		}
		#endregion


		private void Grid_DeleteCommand(object sender, CA.GridItemEventArgs e)
		{
			DeleteCategory(Globals.SafeInt(e.Item["CategoryID"].ToString(), 0)); 
		}
	}
}
